package com.qxgltj.qxgl.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import com.qxgltj.qxgl.dao.IUsersDao;
import com.qxgltj.qxgl.dto.UsersDTO;
import com.qxgltj.qxgl.model.Users;
import com.qxgltj.qxgl.service.IUsersService;

import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
@ApiOperation("会员列表")
@RestController
@RequestMapping(path = "/users",method = RequestMethod.POST)
public class UsersController {
@Resource
private IUsersDao usersDao;
@Resource
private IUsersService usersService;
@ApiOperation(value = "查询所有",notes = "查询所有")
@GetMapping(value = "/findAll")
public Map  findAll(UsersDTO dto){
    List<Users> list=usersDao.findAll();
    Map<String,Object> map=new HashMap<String,Object>();
    map.put("code",0);
    map.put("count",100);
    map.put("msg","查询成功");
    map.put("data", list);
    return map;
}
@ApiOperation(value = "分页查询",notes = "分页查询")
@PostMapping("/page")
public Page findPage(@RequestBody UsersDTO dto){
    System.out.println(dto.toString());
    return usersService.findPage(dto);
}
 @ApiOperation(value = "创建用户",notes = "添加或修改")
 @PostMapping("/addOrEdit")
public Users saveUsers(@RequestBody Users u){
    return usersService.saveUsers(u);
}
@ApiOperation(value = "查询所有用户",notes = "查询所有用户信息")
@GetMapping
public List<Users> findAll(){
    return usersDao.findAll();
}
@ApiOperation(value = "删除用户",notes = "删除用户")
@ApiImplicitParam(name = "id",value = "主键",required = true,paramType = "path")
    @DeleteMapping("/{id}")
    public void del(@PathVariable String id){
        usersService.del(id);
    }
    @ApiOperation(value = "用户设置角色")
    @PostMapping("/andRoles")
    public void setRoles(String user_id,@RequestParam List<String> roleslist){
        usersService.setRoles(user_id,roleslist);
    } 
    @ApiOperation(value = "重置密码",notes = "重置密码")
    @PutMapping(value = "/reset/{id}")
    public int resetpass(@PathVariable String id){
        return usersDao.resetpass(id);
    }   
    @ApiOperation(value = "用户锁定解锁操作")
   @PutMapping(value = "/{islockout}/{id}")
   public Object Users(@PathVariable String islockout,@PathVariable String id){
   int u=usersDao.lock(islockout, id);
   Map<String, Object> map=new HashMap<String, Object>();
   if (u>0) {
       map.put("success", "成功");
   }else{
       map.put("error", "失败");
   }
   return map;
   }
   //删除用户角色
   @ApiOperation(value = "删除用户角色")
   @DeleteMapping(value = "delRoles")
   public int  delRoles(String role_id){
       return usersDao.delRoles(role_id);
   }
}
